home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / MacTCP Library 1.1 / Library / LowLevel ƒ / Source ƒ / UDP.c < prev   
Encoding:
C/C++ Source or Header  |  1995-12-04  |  5.4 KB  |  230 lines  |  [TEXT/SPM ]

  1. /*
  2.     UDP.c
  3.     
  4.     Code to implement the UDP functions.
  5.     
  6.     01/30/94 dn - Created.
  7.     11/28/94 mc - Converted to CodeWarrior
  8.     12/04/95 dn - Prep'd for Apprentice 4
  9. */
  10.  
  11. #include <MyTCPIncludes.h>
  12.  
  13. /*
  14.     UDP_Create
  15.     
  16.     Opens a UDP stream.  It must be called before any UDP datagrams can be sent or received on a particular
  17.     UDP port.  UDP_Create returns a stream pointer that must be used in all subsequent UDP calls that operate
  18.     on this UDP stream.
  19. */
  20. OSErr UDP_Create(UDPiopb* pb,Boolean async){
  21.     if (pb==(UDPiopb*)0)
  22.         return udpNilPB;
  23.     
  24.     if (pb->ioCRefNum==0)
  25.         return udpNilRefNum;
  26.     
  27.     pb->csCode=UDPCreate;
  28.     
  29.     return SyncAsync((ParmBlkPtr)pb,async);
  30. }
  31.  
  32. /*
  33.     UDP_Read
  34.     
  35.     Retrieves a datagram that has been received on the UDP stream defined by the stream pointer.  Some
  36.     number of datagrams are buffered internally within UDP even when no UDP_Read commands are outstanding,
  37.     so it is not necessary to keep a UDP_Read command outstanding at all times.  The exact number of datagrams
  38.     that can be buffered within the MacTCP driver depends on the size of the receive buffer area given to
  39.     MacTCP in the UDP_Create call and the size of the datagrams received.
  40. */
  41. OSErr UDP_Read(UDPiopb* pb,Boolean async){
  42.     if (pb==(UDPiopb*)0)
  43.         return udpNilPB;
  44.     
  45.     if (pb->ioCRefNum==0)
  46.         return udpNilRefNum;
  47.     
  48.     if (pb->udpStream==(StreamPtr)0)
  49.         return udpNilStream;
  50.         
  51.     pb->csCode=UDPRead;
  52.     
  53.     return SyncAsync((ParmBlkPtr)pb,async);
  54. }
  55.  
  56. /*
  57.     UDP_BfrReturn
  58.     
  59.     Returns a receive buffer to the UDP driver that had been passed to you because of a successful
  60.     UDP_Read call that returned a nonzero amount of data.
  61. */
  62. OSErr UDP_BfrReturn(UDPiopb* pb,Boolean async){
  63.     if (pb==(UDPiopb*)0)
  64.         return udpNilPB;
  65.     
  66.     if (pb->ioCRefNum==0)
  67.         return udpNilRefNum;
  68.     
  69.     if (pb->udpStream==(StreamPtr)0)
  70.         return udpNilStream;
  71.         
  72.     pb->csCode=UDPBfrReturn;
  73.     
  74.     return SyncAsync((ParmBlkPtr)pb,async);
  75. }
  76.  
  77. /*
  78.     UDP_Write
  79.     
  80.     Sends a datagram on a UDP stream.
  81. */
  82. OSErr UDP_Write(UDPiopb* pb,Boolean async){
  83.     if (pb==(UDPiopb*)0)
  84.         return udpNilPB;
  85.     
  86.     if (pb->ioCRefNum==0)
  87.         return udpNilRefNum;
  88.     
  89.     if (pb->udpStream==(StreamPtr)0)
  90.         return udpNilStream;
  91.         
  92.     pb->csCode=UDPWrite;
  93.     
  94.     return SyncAsync((ParmBlkPtr)pb,async);
  95. }
  96.  
  97. /*
  98.     UDP_Release
  99.     
  100.     Closes a UDP stream.  Any outstanding commands on that stream are terminated with an error.  The
  101.     ownership of the receive buffer area is used to create the UDP stream passes back to you.
  102. */
  103. OSErr UDP_Release(UDPiopb* pb,Boolean async){
  104.     if (pb==(UDPiopb*)0)
  105.         return udpNilPB;
  106.     
  107.     if (pb->ioCRefNum==0)
  108.         return udpNilRefNum;
  109.     
  110.     if (pb->udpStream==(StreamPtr)0)
  111.         return udpNilStream;
  112.         
  113.     pb->csCode=UDPRelease;
  114.     
  115.     return SyncAsync((ParmBlkPtr)pb,async);
  116. }
  117.  
  118. /*
  119.     UDP_MaxMTUSize
  120.     
  121.     Returns the maximum size of UDP data that can be sent in a single datagram without fragmentation.
  122.     This number does not include the IP and UDP headers.  The value is relative to the destination address.  If
  123.     the address is on the local network, the network MTU size is returned; otherwise, a value of 548 is
  124.     returned.
  125. */
  126. OSErr UDP_MaxMTUSize(UDPiopb* pb,Boolean async){
  127.     if (pb==(UDPiopb*)0)
  128.         return udpNilPB;
  129.     
  130.     if (pb->ioCRefNum==0)
  131.         return udpNilRefNum;
  132.     
  133.     if (pb->udpStream==(StreamPtr)0)
  134.         return udpNilStream;
  135.         
  136.     pb->csCode=UDPMaxMTUSize;
  137.     
  138.     return SyncAsync((ParmBlkPtr)pb,async);
  139. }
  140.  
  141. /*
  142.     UDP_Status
  143.     
  144.     Undocumented.  This routine is not documented in the MacTCP Developers Guide.  It is included for
  145.     the completeness of the library.  I would assume that it works similar to the TCPStatus command.
  146. */
  147. OSErr UDP_Status(UDPiopb* pb,Boolean async){
  148.     if (pb==(UDPiopb*)0)
  149.         return udpNilPB;
  150.     
  151.     if (pb->ioCRefNum==0)
  152.         return udpNilRefNum;
  153.     
  154.     if (pb->udpStream==(StreamPtr)0)
  155.         return udpNilStream;
  156.         
  157.     pb->csCode=UDPStatus;
  158.     
  159.     return SyncAsync((ParmBlkPtr)pb,async);
  160. }
  161.  
  162. /*
  163.     UDP_MultiCreate
  164.     
  165.     Opens UDP connections on a consecutive series of ports.  This routine is similar to UDP_Create except
  166.     that it takes a starting and ending port instead of a single UDP port.  The starting port must not be zero.
  167.     The connection  is established on a range of ports, from the starting port to the ending port, inclusive.
  168.     The UDP_MultiSend routine must be used with this type of connection stream.  Reading datagrams from
  169.     this type of stream is identical to UDP_Read, except that the receiving port may change from datagram
  170.     to datagram.
  171. */
  172. OSErr UDP_MultiCreate(UDPiopb* pb,Boolean async){
  173.     if (pb==(UDPiopb*)0)
  174.         return udpNilPB;
  175.     
  176.     if (pb->ioCRefNum==0)
  177.         return udpNilRefNum;
  178.     
  179.     if (pb->udpStream==(StreamPtr)0)
  180.         return udpNilStream;
  181.     
  182.     if (pb->csParam.create.localPort==0)
  183.         return udpPortNil;
  184.         
  185.     pb->csCode=UDPMultiCreate;
  186.     
  187.     return SyncAsync((ParmBlkPtr)pb,async);
  188. }
  189.  
  190. /*
  191.     UDP_MultiSend
  192.     
  193.     Sends a datagram from a specified port.  (See UDP_Write)
  194. */
  195. OSErr UDP_MultiSend(UDPiopb* pb,Boolean async){
  196.     if (pb==(UDPiopb*)0)
  197.         return udpNilPB;
  198.     
  199.     if (pb->ioCRefNum==0)
  200.         return udpNilRefNum;
  201.     
  202.     if (pb->udpStream==(StreamPtr)0)
  203.         return udpNilStream;
  204.         
  205.     pb->csCode=UDPMultiSend;
  206.     
  207.     return SyncAsync((ParmBlkPtr)pb,async);
  208. }
  209.  
  210. /*
  211.     UDP_MultiRead
  212.     
  213.     Receives data from a port created with the UDP_MultiCreate command.  The port and host address on which
  214.     the packet was received is specified in the destination port and destination host parameter-block fields.
  215. */
  216. OSErr UDP_MultiRead(UDPiopb* pb,Boolean async){
  217.     if (pb==(UDPiopb*)0)
  218.         return udpNilPB;
  219.     
  220.     if (pb->ioCRefNum==0)
  221.         return udpNilRefNum;
  222.     
  223.     if (pb->udpStream==(StreamPtr)0)
  224.         return udpNilStream;
  225.         
  226.     pb->csCode=UDPMultiRead;
  227.     
  228.     return SyncAsync((ParmBlkPtr)pb,async);
  229. }
  230.